#include "stdafx.h"
#include "./stageobj.h"

#define BKG_NAME "bkg"

//btn_speedup
#define BTN_SPEEDUP_NAME "btn_speedup"
#define BTN_SPEEDUP_POS Point(78, 95)

//btn_speeddown
#define BTN_SPEEDDOWN_NAME "btn_speeddown"
#define BTN_SPEEDDOWN_POS Point(85, 95)

//btn_menu
#define BTN_MENU_NAME "btn_menu"
#define BTN_MENU_POS Point(93, 95)

#define MAX_SPEED 5
#define MIN_SPEED 1

#define SPEED_TEXT_POS Point(78, 92)
#define SPEEDNUM_TEXT_POS Point(87, 92)

DEF_MODEL_NAME(StageObj, "scene")

StageObj::StageObj(float depth)
: m_btnSpeedUp(BTN_SPEEDUP_NAME, BTN_SPEEDUP_POS, depth)
, m_btnSpeedDown(BTN_SPEEDDOWN_NAME, BTN_SPEEDDOWN_POS, depth)
, m_btnMenu(BTN_MENU_NAME, BTN_MENU_POS, depth)
, m_speed(1)
, m_TextSpeed("Game Speed:", SPEED_TEXT_POS, depth, 9, Color(0, 0, 0))
, m_Text1("1", SPEEDNUM_TEXT_POS, depth, 9, Color(0, 0, 0))
, m_Text2("2", SPEEDNUM_TEXT_POS, depth, 9, Color(0, 0, 0))
, m_Text3("3", SPEEDNUM_TEXT_POS, depth, 9, Color(0, 0, 0))
, m_Text4("4", SPEEDNUM_TEXT_POS, depth, 9, Color(0, 0, 0))
, m_Text5("5", SPEEDNUM_TEXT_POS, depth, 9, Color(0, 0, 0))
{
	m_btnSpeedUp.OnClick += EventObject<OnButtonClickFunc>(this, &StageObj::OnBtnSpeedUpClick);
	m_btnSpeedDown.OnClick += EventObject<OnButtonClickFunc>(this, &StageObj::OnBtnSpeedDownClick);
	m_btnMenu.OnClick += EventObject<OnButtonClickFunc>(this, &StageObj::OnBtnMenuClick);

	m_btnSpeedDown.SetEnable(false);
	m_btnSpeedUp.SetEnable(false);

	SetSpeedText();
}

StageObj::~StageObj()
{
	TimeUtils::SetDifferMultNum(1);
}

void StageObj::OnRender(float differTime)
{
	m_model.Render();
}

void StageObj::OnBtnSpeedUpClick()
{
	if (m_speed == MIN_SPEED)
		return;

	TimeUtils::SetDifferMultNum(--m_speed);
	SetSpeedText();
}

void StageObj::OnBtnSpeedDownClick()
{
	if (m_speed == MAX_SPEED)
		return;

	TimeUtils::SetDifferMultNum(++m_speed);
	SetSpeedText();
}

void StageObj::OnBtnMenuClick()
{
	FireEvent(OnStageBtnMenuClickFunc, OnMenuClick, ());
}

void StageObj::SetPlayerLights(vector3d* playerPos, size_t playerCount)
{
	unsigned int lightnum = GL_LIGHT0;
	for (size_t i = 0; i < playerCount; ++i)
	{
		m_light.SetVerticalLight(lightnum + i, playerPos[i]);
	}
}

void StageObj::StartLights()
{
	m_light.SetLights();
	//start
	TimeUtils::SetDifferMultNum(m_speed);

	m_btnSpeedDown.SetEnable(true);
	m_btnSpeedUp.SetEnable(true);
}

void StageObj::ShutDownStage()
{
	m_light.ShutDownLights();

	m_btnSpeedDown.SetEnable(false);
	m_btnSpeedUp.SetEnable(false);
}

void StageObj::SetSpeedText()
{
	m_Text1.SetVisible(false);
	m_Text2.SetVisible(false);
	m_Text3.SetVisible(false);
	m_Text4.SetVisible(false);
	m_Text5.SetVisible(false);

	switch (m_speed)
	{
	case 5:
		m_Text1.SetVisible(true);
		break;
	case 4:
		m_Text2.SetVisible(true);
		break;
	case 3:
		m_Text3.SetVisible(true);
		break;
	case 2:
		m_Text4.SetVisible(true);
		break;
	case 1:
		m_Text5.SetVisible(true);
		break;
	}
}